
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Security;
using Swaf.Gui;

namespace FilterSort.Gui
{
	public class FilterSortBizProc : BaseBusinessProcess
	{
        protected string m_sessionId;
        protected string m_sessionGroupName;
		protected string m_groupName;

		public FilterSortBizProc(ActiveBizProc procState)
			: base(procState)
		{
		}

        protected override void setupRuleNames()
        {
            base.m_deleteBizObjRule = "deleteFilterSortSave";
            base.m_getBizObjDetailsRule = "getFilterSortSaveDetails";
			base.m_getBizObjListRule = "getFilterSortSaves";
			base.m_updateBizObjRule = "addUpdateFilterSortSave";
		}

		protected override object[] getBizObjListRuleParameters(object parentId, IBizObj filterSort)
		{
			return new object[] { getGroupName() };
		}

		protected override Privileges CurrentEditPrivilege
		{
			get { return CurrentPrincipal.getPrivilege("AppFunctions.ManageUsers"); }
		}

		protected virtual string getGroupName()
		{
			return "testGroupName";
		}


		protected override void setupActions()
		{
			Privileges curPriv = CurrentEditPrivilege;
			base.setupActions();
			Actions.addAction(new GenericAction("Save", true, (curPriv > Privileges.Edit), this, new ActionParamTrigger(updateBizObj), new object[] { }));
		}

		public override void updateActionState()
		{
			base.updateActionState();
			((GenericAction)Actions["Save"]).Enabled = m_currentBizObj == null ? false : true;
		}

		public void updateBizObj(object obj)
		{
			m_updateBizObjRule = "addUpdateFilterSortSave";
			base.updateBizObj((IBizObj)obj);
			base.refreshData();
		}

		protected override void getCurrentBizObj()
		{
			if ((m_currentBizObj = m_procState.OfflineState.State.get("criteria") as IBizObj) == null)
			{
				base.getCurrentBizObj();
				m_procState.OfflineState.State.put("criteria", m_currentBizObj);
			}
		}
		//public override void initiateDataRetrieval(ParentBizProcInfo parent)
		//{
		//    // the sessionId that where going to use along the way
		//    m_sessionId = string.Format("{0}-Data", this.Id);
		//    m_sessionGroupName = string.Format("{0}-GroupName", this.Id);
		//    base.m_parentBizProc = parent;

		//    // note: hardcoded groupName for now.
		//    string groupName = getGroupName();
		//    App.sessionData[m_sessionGroupName] = groupName;
		//    base.initiateDataRetrieval(m_baseRule, groupName);
		//}


		//protected override void updateEditPriv()
		//{
		//    m_inEditMode = true;
		//    m_editPriv = CurrentPrincipal.getPrivilege("AppFunctions.ManageUsers");
		//}

		//protected override void setupActions()
		//{
		//    base.setupActions();
		//    Actions.addAction(new GenericAction("Save", true, (m_editPriv > Privileges.Edit),this , new ActionParamTrigger(updateBizObj), new object[] { }));
		//}

		//public override void updateActionState()
		//{
		//    base.updateActionState();
		//    ((GenericAction)Actions["Save"]).Enabled = m_currentBizObj == null ? false : true;
		//}

		//public void updateBizObj(object obj)
		//{
		//    m_updateBizObjRule = "addUpdateFilterSortSave";
		//    base.updateBizObj((IBizObj)obj);
		//    base.refreshData();
		//}

		//public override void useData(IBizObj bizObj)
		//{
		//    if (((IBizObjList)bizObj.get("options.filterOptions")).numElements == 0)
		//        ((IBizObjList)bizObj.get("options.filterOptions")).insertNew();
		//    App.sessionData[m_sessionId] = bizObj;
		//    base.useData(bizObj);
		//}

		//protected override void preProcessResults(ref IBizObjList list)
		//{
		//    IBizObj curSelected = App.sessionData[m_sessionId] as IBizObj;
		//    if (curSelected != null)
		//        for(int pos = 0; pos < list.numElements; pos++)
		//            if (((IComparable)list[pos].KeyValue).CompareTo(curSelected.KeyValue) == 0)
		//            {
		//                ((Swaf.BizObj.basic.FullBizObjList)list)[pos] = curSelected;
		//                return;
		//            }
		//}

		//protected override IBizObj calculateSelectedBizObj()
		//{
		//    return App.sessionData[m_sessionId] as IBizObj;
		//}

		//protected override void listDisplay_BizObjSelected(object source, IBizObj selectedBizObj, string info)
		//{
		//    App.sessionData[m_sessionId] = null;
		//    base.listDisplay_BizObjSelected(source, selectedBizObj, info);
			
		//}
		//public override IBizObj getBizObjDetails(IBizObj details)
		//{
		//    if (details.Equals(App.sessionData[m_sessionId]))
		//        return details;
		//    return base.getBizObjDetails(details);
		//}

    }
}